package com.yidu.service.impl;

import java.math.BigDecimal;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;

import org.springframework.stereotype.Service;

import com.yidu.comment.Tools;
import com.yidu.dao.SnComInDetailMapper;
import com.yidu.dao.SnComInventoryMapper;
import com.yidu.dao.SnCommodityMapper;
import com.yidu.dao.SnProductionMapper;
import com.yidu.dao.SnQuDetailMapper;
import com.yidu.dao.SnQualityMapper;
import com.yidu.model.SnComInDetail;
import com.yidu.model.SnComInventory;
import com.yidu.model.SnCommodity;
import com.yidu.model.SnProDetail;
import com.yidu.model.SnProduction;
import com.yidu.model.SnQuDetail;
import com.yidu.model.SnQuality;
import com.yidu.service.SnQuDetailService;

/**
 * 生产质检详情实现类
 * @author chensituo
 * @date 2018年12月14日 下午1:49:01
 */
@Service
public class SnQuDetailServiceImpl implements SnQuDetailService{
	/**
	 * 注入生产质检详情mapper
	 */
	@Resource
	SnQuDetailMapper quDetailMapper;
	/**
	 * 注入生产质检mapper
	 */
	@Resource
	SnQualityMapper qualityMapper;
	/**
	 * 注入商品库存
	 */
	@Resource
	SnComInventoryMapper comInventoryMapper;

	/**
	 * 注入商品库存记录mapper
	 */
	@Resource
	SnComInDetailMapper comInDetailMapper;
	/**
	 * 注入商品mapper
	 */
	@Resource
	SnCommodityMapper commodityMapper;
	/**
	 * 注入生产计划mapper
	 */
	@Resource
	SnProductionMapper productionMapper;
	@Override
	public List<SnProDetail> findAll(Map<String, Object> maps) {
		//调用查询所有的方法
		return quDetailMapper.findAll(maps);
	}

	@Override
	public int findCount(Map<String, Object> maps) {
		//调用查询行数的方法
		return quDetailMapper.findCount(maps);
	}

	@Override
	public int updateNotCount(String sum) {
		//将字符串根据@拆分
		String [] strings=sum.split("@");
		int rows=0;
		String quality="";
		int aleady=0;
		int notcount=0;
		//遍历
		for (String string : strings) {
			//根据，拆分
			String [] arr= string.split(",");
			//根据生产质检详情id查询的到对象
			SnQuDetail snQuDetail=quDetailMapper.selectByPrimaryKey(arr[0]);
			//将对象值修改
			snQuDetail.setQdNotcount(Integer.valueOf(arr[1]));
			//得到所有没通过的数量
			notcount+=Integer.valueOf(arr[1]);
			snQuDetail.setQdAlready(snQuDetail.getQdCount()-snQuDetail.getQdNotcount());
			//得到所有通过的数量
			aleady+=snQuDetail.getQdAlready();
			snQuDetail.setQdProbability(Math.round(((Double.valueOf(snQuDetail.getQdAlready())/Double.valueOf(snQuDetail.getQdCount()))*100))+"%");
			rows=quDetailMapper.updateByPrimaryKeySelective(snQuDetail);
			//将生产质检id赋值
			quality=snQuDetail.getQuId();
		}
		//根据质检id查询
		SnQuality snQuality=qualityMapper.selectByPrimaryKey(quality);
		//将状态修改为1
		snQuality.setOper("1");
		snQuality.setQuAlready(aleady);
		snQuality.setQuNotcount(notcount);
		snQuality.setQuProbability(((Double.valueOf(snQuality.getQuAlready())/Double.valueOf(snQuality.getQuCount()))*100)+"%");
		int row=qualityMapper.updateByPrimaryKeySelective(snQuality);
		//根据生产id插叙
		SnProduction production=productionMapper.selectByPrimaryKey(snQuality.getProId());
		//将状态修改为6
		production.setProState(6);
		int rowsa=productionMapper.updateByPrimaryKeySelective(production);
		//如果都成功返回1
		if(rows==1 && row==1 && rowsa==1) {
			return 1;
		}else {
			return 0;
		}
	}

	@Override
	public int updateSh(String quId) {
		//根据质检id查询
		SnQuality quality=qualityMapper.selectByPrimaryKey(quId);
		//将状态修改为3
		quality.setOper("3");
		int ro=qualityMapper.updateByPrimaryKeySelective(quality);
		//根据质检id查询商品id和质检数量
		List<Map<String, Object>> inte=qualityMapper.selectComId(quId);
		//去重复
		for  ( int  i  =   0 ; i  <  inte.size()  -   1 ; i ++ )  {       
			for  ( int  j  =  inte.size()  -   1 ; j  >  i; j -- )  {       
				if  (inte.get(j).get("qd_id").equals(inte.get(i).get("qd_id")))  {       
					inte.remove(j);       
				}        
			}        
		}  
		int rows=0;
		int row=0;
		for (Map<String, Object> map : inte) {
			System.err.println(map.get("com_id")+"----"+map.get("qd_already"));
			//调用根据商品id查询商品的方法
			SnCommodity commodity=commodityMapper.selectByPrimaryKey((String) map.get("com_id"));
			//增加商品库存

			Map<String, Object> map2=new HashMap<>();
			map.put("comId", (String) map.get("com_id"));
			map.put("braId","123");
			//根据商品id查询 总公司的库存
			SnComInventory comInventory=comInventoryMapper.findComBranId(map);
			
			//如果不为空
			if(comInventory!=null && !"".equals(comInventory)) {
				System.err.println("------------------"+map.get("qd_already"));
				System.out.println("*****"+comInventory.getCiNumber()+"****"+map.get("qd_already"));
				comInventory.setCiNumber(new BigDecimal(comInventory.getCiNumber().intValue()+Integer.valueOf(map.get("qd_already").toString())));
				//调用修改商品库存的方法
				rows=comInventoryMapper.updateByPrimaryKeySelective(comInventory);
				
				//创建商品库存记录对象
				SnComInDetail comInDetail=new SnComInDetail();
				comInDetail.setSort(Integer.valueOf(Tools.getTimeStamp()));
				comInDetail.setCiId(comInventory.getCiId());
				comInDetail.setCidId(Tools.getDateOrderNo());
				comInDetail.setCidExplain("生产"+commodity.getComName()+"增加"+commodity.getComName()+"库存"+map.get("qd_already")+commodity.getComUnit());
				comInDetail.setOptime(Tools.getCurDateTime());
				comInDetail.setIsva(1);
				//调用增加商品库存记录的方法
				row=comInDetailMapper.insertSelective(comInDetail);
			}else {
				System.out.println("--------------------增加库存------------------");
			}
		}
		if(ro==1 &&  rows==1 && row==1) {
			return 1;
		}else {
			return 0;
		}
		
	}
}
